package com.apesource.bookBuy.mapper;

import com.apesource.bookBuy.bean.Book;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @auther:暮雪
 * @date:2025/8/26
 */
public interface BookMapper {
    //查询所有图书
    @Select("select * from book")
    List<Book> selectAllBook();

    @Select("select * from book where book_name=#{bookName} and book_price=#{bookPrice} and book_author=#{bookAuthor}")
    //通过所有信息查询图书
    Book getBookByAllInfo(Book book);

    @Select("select * from book where book_id=#{bid}")
    //通过id获取图书
    Book getBookById(int id);


    @Select("<script>" +
            "select * from book " +
            "<where> " +
            "<trim suffixOverrides=\"and\"> " +
            "<if test=\"authorKey!= null\"> " +
            "book_author like concat('%', #{authorKey}, '%') and " +
            "</if> " +
            "<if test=\"bookNameKey!= null\"> " +
            "book_name like concat('%', #{bookNameKey}, '%') " +
            "</if> " +
            "</trim> " +
            "</where> " +
            "</script>")
    List<Book> getBooksByAuthorAndBookName(@Param("authorKey") String authorKey, @Param("bookNameKey") String bookNameKey);


    //查询哪些书被全部借走了
    @Select("select * from book where book_num =0")
    List<Book> getBooksByBookNumIsEmpty();

    //添加图书
    @Insert("insert into book(book_name,book_price,book_author,book_num)       values (#{bookName},#{bookPrice},#{bookAuthor},#{bookNum})")
    int insertBook(Book book);

    //修改图书
    @Update("update book set book_name=#{bookName},book_price=#{bookPrice},book_num=#{bookNum},book_author=#{bookAuthor} where book_id=#{bookId}")
    int updateBook(Book book);

    //修改图书库存
    @Update("update book set book_num=#{param2} where book_id=#{param1}")
    int updateBookNum(int bid,int bookNum);

    //删除图书
    @Delete("delete from book where book_id=#{bookId}")
    int deleteBookById(int id);


}
